import Message from '@arco-design/web-vue/es/message'
import { isString } from 'lodash'

export const copy = async (text?: string) => {
  if (!isString(text) || !text) {
    Message.error('无复制内容')
    return
  }
  const textarea = document.createElement('textarea')
  textarea.className = 'fixed'
  textarea.value = isString(text) ? text : ''
  document.body.appendChild(textarea)
  const active = document.activeElement as HTMLElement
  textarea.focus()
  textarea.select()

  try {
    document.execCommand('copy')
    Message.success('复制成功')
  } catch (e: any) {
    Message.error('无复制内容')
  } finally {
    document.body.removeChild(textarea)
    active.blur()
  }
}
